const { spawn } = require('child_process')
const path = require('path')
const axios = require('axios')

const filePath = '/Applications/飞项.app/Contents/MacOS/飞项'
// const filePath = 'C:/Program Files/ynote-desktop/有道云笔记.exe'
// const filePath = 'C:/Users/home/AppData/Local/Feishu/Feishu.exe'


const ports = {
  node: 8888,
  pages: 8889,
}


const c = spawn(filePath, [
  `--inspect=${ports.node}`,
  `-remote-debugging-port=${ports.pages}`
],
  {
    cwd: path.dirname(filePath)
  }
)

c.on('data', (...args) => {
  console.log('标准输出参数', args)
})


const getUrls = async () => {
  const res = []

  try {

    for (const key of Object.keys(ports)) {
      let configs = await axios.get(`http://127.0.0.1:${ports[key]}/json`)

      for (const config of configs.data) {
        const devUrl = config.devtoolsFrontendUrl.replace(/\/devtools\//, 'devtools://devtools/bundled/')

        res.push({
          ...config,
          devUrl,
        })
      }
    }
  } catch (err) {
    console.log(err)
  }

  console.log(res)
}

setTimeout(() => {
  getUrls()
}, 3000)


